<template>
  <div class="container">
    <h3 class="title"><span />数据库备份列表</h3>
    <el-button
      icon="el-icon-plus"
      size="small"
      style="margin-bottom:20px"
      type="primary"
      class="add five"
      @click="backup"
    >备份数据库</el-button>
    <el-table v-loading="loading" :data="dataBase">
      <!-- <el-table-column label="ID" prop="id" /> -->
      <el-table-column label="文件名" prop="title" />
      <el-table-column label="文件大小" prop="size" />
      <el-table-column label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="restore(scope.row)">还原</el-button>
          <el-button class="dele" size="mini" @click="getDele(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import baseApi from '@/api/log'
export default {
  data() {
    return {
      dataBase: [],
      loading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 数据库备份
    backup() {
      this.loading = true
      baseApi.dataBackup().then(res => {
        this.loading = false
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 数据库文件列表
    getList() {
      baseApi.dataList().then(res => {
        this.dataBase = res.data
      })
    },
    // 还原
    restore(row) {
      baseApi.dataRestore({ filename: row.title }).then((res) => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 删除
    getDele(row) {
      this.$confirm('此操作将永久删除该备份文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        baseApi.dataDelete({ filename: row.title }).then((res) => {
          if (res.code == 0) {
            this.getList()
            this.$message.success(res.msg)
          } else {
            this.$message.error(res.msg)
          }
        })
          .catch((res) => {
            this.$message.error(res.msg)
          })
      })
        .catch((rej) => {
          this.$message.info('已取消')
        })
    }
  }
}
</script>

<style scoped>

</style>
